package net.caixiaomi.info.ui.football;

import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.view.View;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.ElementCellModel;
import net.caixiaomi.info.model.MatchPlaysList;
import net.caixiaomi.info.model.PlayFootballItem;
import net.caixiaomi.info.util.Constants;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 胜平负
 */

public class TakeOrderVictoryAndDefeatProxy extends TakeOrderItemViewProxy<MultiItemEntity, BaseViewHolder> {
    private FootballTakeOrderAdapter mAdapter;

    public TakeOrderVictoryAndDefeatProxy(FootballTakeOrderAdapter adapter) {
        super(adapter);
        mAdapter = adapter;
    }

    @Override
    public void bindView(BaseViewHolder helper, MultiItemEntity item) {
        try {
            final PlayFootballItem model = (PlayFootballItem) item;
            MatchPlaysList matchPlaysList = model.getMatchPlays().get(0);

            helper.setVisible(R.id.action, TextUtils.equals(matchPlaysList.getSingle(), "1"));

            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            String time = sdf.format(new Date(Long.parseLong(model.getBetEndTime()) * 1000));
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(model.getLeagueAddr()).append(" ").append(model.getChangci()).append(" ").append(CommonApp.getInstance().getString(R.string.C_END_TIME)).append(time);
            helper.setText(R.id.text, stringBuilder);
            stringBuilder.setLength(0);

            ElementCellModel homeCell = matchPlaysList.getHomeCell();
            stringBuilder.append(homeCell.getCellName()).append(homeCell.getCellOdds());
            String term = stringBuilder.toString();
            stringBuilder.setLength(0);
            if (!TextUtils.isEmpty(model.getHomeTeamRank())) {
                stringBuilder.append("[").append(model.getHomeTeamRank()).append("]");
            }
            stringBuilder.append(model.getHomeTeamAbbr());
            stringBuilder.append("\n").append(term);
            SpannableString spannableString = new SpannableString(stringBuilder);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), homeCell.isSelected() ? R.color.white : R.color.third_text)), stringBuilder.length() - term.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(0.8f), stringBuilder.length() - term.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            helper.getView(R.id.host).setSelected(homeCell.isSelected());
            helper.setText(R.id.host, spannableString);

            helper.setTag(R.id.host, R.id.item_model, model);
            helper.setTag(R.id.host, R.id.item_cell, homeCell);

            helper.setTextColor(R.id.host, ContextCompat.getColor(CommonApp.getInstance(), homeCell.isSelected() ? R.color.white : R.color.primary_text));
            helper.setOnClickListener(R.id.host, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                    PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);
                    if (mAdapter.getListener() != null) {
                        if (mAdapter.getListener().canChange(tagModel)) {
                            tag.setSelected(!tag.isSelected());
                            if (tagModel.getSelectCell() == null) {
                                tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                            }
                            if (tag.isSelected()) {
                                tagModel.getSelectCell().add(tag);
                            } else {
                                tagModel.getSelectCell().remove(tag);
                            }
                            mAdapter.getListener().onItemChangeListener(tagModel);
                        }
                    }
                }
            });

            stringBuilder.setLength(0);
            ElementCellModel flatCell = matchPlaysList.getFlatCell();
            stringBuilder.append(flatCell.getCellName()).append(flatCell.getCellOdds());
            term = stringBuilder.toString();
            stringBuilder.setLength(0);
            String fixedOdds = matchPlaysList.getFixedOdds();//+1 、-1
            if (TextUtils.isEmpty(fixedOdds)) {
                stringBuilder.append("VS").append("\n").append(term);
            } else {
                stringBuilder.append("VS ").append(fixedOdds).append("\n").append(term);
            }

            spannableString = new SpannableString(stringBuilder);
            if (!TextUtils.isEmpty(fixedOdds)) {
                int start = stringBuilder.indexOf(fixedOdds);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), flatCell.isSelected() ? R.color.white : fixedOdds.startsWith("+") ? R.color.orange_primary : R.color.green)), start, start + fixedOdds.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, start + fixedOdds.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), flatCell.isSelected() ? R.color.white : R.color.third_text)), stringBuilder.length() - term.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(0.8f), stringBuilder.length() - term.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);

            helper.setText(R.id.tie, spannableString);
            helper.getView(R.id.tie).setSelected(flatCell.isSelected());

            helper.setTag(R.id.tie, R.id.item_model, model);
            helper.setTag(R.id.tie, R.id.item_cell, flatCell);

            helper.setTextColor(R.id.tie, ContextCompat.getColor(CommonApp.getInstance(), flatCell.isSelected() ? R.color.white : R.color.third_text));
            helper.setOnClickListener(R.id.tie, new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                    PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);

                    if (mAdapter.getListener() != null) {
                        if (mAdapter.getListener().canChange(tagModel)) {
                            tag.setSelected(!tag.isSelected());
                            if (tagModel.getSelectCell() == null) {
                                tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                            }
                            if (tag.isSelected()) {
                                tagModel.getSelectCell().add(tag);
                            } else {
                                tagModel.getSelectCell().remove(tag);
                            }
                            mAdapter.getListener().onItemChangeListener(tagModel);
                        }
                    }
                }
            });

            ElementCellModel visitingCell = matchPlaysList.getVisitingCell();
            stringBuilder.setLength(0);
            stringBuilder.append(visitingCell.getCellName()).append(visitingCell.getCellOdds());
            term = stringBuilder.toString();
            stringBuilder.setLength(0);
            if (!TextUtils.isEmpty(model.getVisitingTeamRank())) {
                stringBuilder.append("[").append(model.getVisitingTeamRank()).append("]");
            }
            stringBuilder.append(model.getVisitingTeamAbbr());
            stringBuilder.append("\n").append(term);
            spannableString = new SpannableString(stringBuilder);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), visitingCell.isSelected() ? R.color.white : R.color.third_text)), stringBuilder.length() - term.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(0.8f), stringBuilder.length() - term.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            helper.setText(R.id.guest, spannableString);

            helper.setTag(R.id.guest, R.id.item_model, model);
            helper.setTag(R.id.guest, R.id.item_cell, visitingCell);

            helper.setTextColor(R.id.guest, ContextCompat.getColor(CommonApp.getInstance(), visitingCell.isSelected() ? R.color.white : R.color.primary_text));
            helper.getView(R.id.guest).setSelected(visitingCell.isSelected());
            helper.setOnClickListener(R.id.guest, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                    PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);
                    if (mAdapter.getListener() != null) {
                        if (mAdapter.getListener().canChange(tagModel)) {
                            tag.setSelected(!tag.isSelected());
                            if (tagModel.getSelectCell() == null) {
                                tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                            }
                            if (tag.isSelected()) {
                                tagModel.getSelectCell().add(tag);
                            } else {
                                tagModel.getSelectCell().remove(tag);
                            }
                            mAdapter.getListener().onItemChangeListener(tagModel);
                        }
                    }
                }
            });

            helper.addOnClickListener(R.id.btn_delete);

            helper.addOnClickListener(R.id.btn_bold);
            helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));

            if (homeCell.isSelected() || flatCell.isSelected() || visitingCell.isSelected()) {
                helper.getView(R.id.btn_bold).setEnabled(true);
                if (model.isBlod()) {
                    helper.getView(R.id.btn_bold).setSelected(true);
                    helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary));
                } else {
                    helper.getView(R.id.btn_bold).setSelected(false);
                    if (mAdapter.getMaxDan() > 0) {
                        helper.getView(R.id.btn_bold).setEnabled(true);
                    } else {
                        helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.light_gray));
                        helper.getView(R.id.btn_bold).setEnabled(false);
                    }
                }
            } else {
                helper.getView(R.id.btn_bold).setEnabled(false);
                helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.light_gray));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addItemType() {
        mAdapter.addItemType(Constants.ITEM_VIEW_ITEM, R.layout.item_take_order_victory_and_defeat);
    }
}
